package com.rul.mars.user.service.impl;

import com.rul.mars.common.service.RedisService;
import com.rul.mars.mbg.model.User;
import com.rul.mars.user.service.UserCacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 用户缓存服务实现类
 *
 * @author RuL
 */
@Service
public class UserCacheServiceImpl implements UserCacheService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserCacheServiceImpl.class);

    @Autowired
    private RedisService redisService;

    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.key.authCode}")
    private String REDIS_KEY_AUTH_CODE;
    @Value("${redis.key.user}")
    private String REDIS_KEY_USER;
    @Value("${redis.expire.common}")
    private long REDIS_EXPIRE_COMMON;
    @Value("${redis.expire.authCode}")
    private long REDIS_EXPIRE_AUTH_CODE;

    /**
     * 添加用户缓存
     *
     * @param user 用户信息
     */
    @Override
    public void setUser(User user) {
        String key = REDIS_DATABASE + REDIS_KEY_USER + user.getId();
        redisService.set(key, user, REDIS_EXPIRE_COMMON);
    }

    /**
     * 删除用户缓存
     *
     * @param userId 用户ID
     */
    @Override
    public void deleteUser(Long userId) {
        String key = REDIS_DATABASE + REDIS_KEY_USER + userId;
        redisService.del(key);
    }

    /**
     * 查询用户缓存
     *
     * @param userId 用户ID
     * @return 相应的用户信息
     */
    @Override
    public User getUser(Long userId) {
        String key = REDIS_DATABASE + REDIS_KEY_USER + userId;
        return (User) redisService.get(key);
    }

    /**
     * 添加手机验证码缓存
     *
     * @param phone    手机号
     * @param authCode 验证码
     */
    @Override
    public void setAuthCode(String phone, String authCode) {
        String key = REDIS_DATABASE + REDIS_KEY_AUTH_CODE + phone;
        redisService.set(key, authCode, REDIS_EXPIRE_AUTH_CODE);
    }

    /**
     * 查询手机验证码缓存
     *
     * @param phone 手机号
     * @return 手机验证码
     */
    @Override
    public String getAuthCode(String phone) {
        String key = REDIS_DATABASE + REDIS_KEY_AUTH_CODE + phone;
        return (String) redisService.get(key);
    }
}
